package io.babystar.modules.business.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.babystar.common.utils.PageUtils;
import io.babystar.common.utils.R;
import io.babystar.common.utils.ShiroUtils;
import io.babystar.modules.business.entity.BaseChannelEntity;
import io.babystar.modules.business.service.BaseChannelService;
import io.babystar.modules.sys.entity.SysUserEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;



/**
 * 渠道表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-06-18 14:59:49
 */
@RestController
@RequestMapping("business/basechannel")
public class BaseChannelController {
    @Autowired
    private BaseChannelService baseChannelService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("business:basechannel:list")
    public R list(@RequestParam Map<String, Object> params){
        SysUserEntity userEntity = (SysUserEntity) ShiroUtils.getSubject().getPrincipal();
        params.put("businessId",userEntity.getBusinessId().toString());
        PageUtils page = baseChannelService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("business:basechannel:info")
    public R info(@PathVariable("id") Integer id){
		BaseChannelEntity baseChannel = baseChannelService.getById(id);

        return R.ok().put("baseChannel", baseChannel);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("business:basechannel:save")
    public R save(@RequestBody BaseChannelEntity baseChannel){
        SysUserEntity userEntity = (SysUserEntity) ShiroUtils.getSubject().getPrincipal();
        baseChannel.setBusinessId(userEntity.getBusinessId().intValue());
        baseChannel.setCreator(userEntity.getUserId().toString());
        baseChannel.setCreateTime(new Date());
		baseChannelService.save(baseChannel);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("business:basechannel:update")
    public R update(@RequestBody BaseChannelEntity baseChannel){
        SysUserEntity userEntity = (SysUserEntity) ShiroUtils.getSubject().getPrincipal();
        baseChannel.setBusinessId(userEntity.getBusinessId().intValue());
        baseChannel.setUpdator(userEntity.getUserId().toString());
        baseChannel.setUpdateTime(new Date());
        baseChannelService.updateById(baseChannel);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("business:basechannel:delete")
    public R delete(@RequestBody Integer[] ids){
		baseChannelService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 根据机构查询
     */
    @RequestMapping("/queryList")
    public R queryList(){
        SysUserEntity userEntity = (SysUserEntity) ShiroUtils.getSubject().getPrincipal();
        List<BaseChannelEntity> list = baseChannelService.list(new QueryWrapper<BaseChannelEntity>().eq("business_id", userEntity.getBusinessId()));

        return R.ok().put("list", list);
    }
}
